﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProTrack.Api.Helper;
using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IServices.Plans;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProTrack.Api.Controllers
{

    /// <summary>
    /// 排班日历服务接口
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class PlanCalendarController : ControllerBase
    {
        public PlanCalendarController(IPlanCalendarServices calendarServices, LogHelper LogHelper)
        {
            CalendarServices = calendarServices;
            this.LogHelper = LogHelper;
        }

        public IPlanCalendarServices CalendarServices { get; }
        public LogHelper LogHelper { get; }



        /// <summary>
        /// 根据月份获取排班计划
        /// </summary>
        /// <param name="Date">当前年份月份的排班计划</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetPlan([FromQuery]string Date)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var list = await CalendarServices.GetPlanAsync(Date);
                //清洗数据

                var new_list=GetPlans(list.ToList());

                status.Data = new_list;
                status.Count = new_list.Count();
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("排版日历-获取当前月份的排班数量", ex, HttpContext);
            }
            return Ok(status);
        }


        private List<PlanCalendarDto> GetPlans(List<PlanTeam> list)
        {
            List<PlanCalendarDto> planList = new List<PlanCalendarDto>();

            if (list.Any())
            {
                var maxDate = list.Max(x => x.EndDate);
                var minDate = list.Min(x => x.StartDate);

                //计算相隔天数 得到循环次数
                var day = maxDate.Subtract(minDate).Days;

                var currDate = new DateTime(minDate.Year, minDate.Month, minDate.Day, 00, 00, 00);
                for (int i = 0; i < day + 1; i++)
                {
                    var tList = list.Where(x => currDate >= new DateTime(x.StartDate.Year, x.StartDate.Month, x.StartDate.Day, 00, 00, 00) && currDate <= new DateTime(x.EndDate.Year, x.EndDate.Month, x.EndDate.Day, 00, 00, 00)).ToList();

                    if (tList.Count > 0)
                    {
                        planList.Add(new PlanCalendarDto { Count = tList.Count, Date = currDate.ToString("yyyy-MM-dd HH:mm:ss") });
                    }

                    //如果当前日期小于 最大日期 则 +1天
                    if (currDate < maxDate)
                    {
                        currDate = currDate.AddDays(1);
                    }
                }
            }
           

            return planList;
        }




        /// <summary>
        /// 根据年月日获取当天的排班信息
        /// </summary>
        /// <param name="PageIndex">当前页数</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="Date">日期</param>
        /// <param name="ShiftWay">轮班方式</param>
        /// <param name="TeamName">班组名称</param>
        /// <returns></returns>
        [HttpGet("GetCurrentPlan")]
        public async Task<IActionResult> GetCurrentPlan([FromQuery] int PageIndex, [FromQuery] int PageSize, [FromQuery] string Date, [FromQuery] string ShiftWay, [FromQuery] string TeamName)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var list = await CalendarServices.GetCurrentPlanAsync(PageIndex, PageSize, Date, ShiftWay, TeamName);
                var count = await CalendarServices.GetCurrentPlanCountAsync(Date, ShiftWay, TeamName);

                status.Data = list;
                status.Count = count;
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("排版日历-根据日期获取排班", ex, HttpContext);
            }
            return Ok(status);
        }
    }
}
